package com.example.beikeyigou.login;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.beikeyigou.R;
import com.example.beikeyigou.untils.BaseFragment;
import com.example.beikeyigou.untils.SaveUserUtils;
import com.example.beikeyigou.untils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/*
* 注册
* */
public class LoginFragment extends BaseFragment {

    EditText telEt,nicknameEt,pwdEt,repwdEt;
    Button ensureBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_login, container, false);

        initView(view);
        return view;
    }

    private void initView(View view) {
        telEt = view.findViewById(R.id.frag_login_et_tel);
        pwdEt = view.findViewById(R.id.frag_login_et_pwd);
        ensureBtn = view.findViewById(R.id.frag_login_btn);
        ensureBtn.setOnClickListener(listener);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String tel = telEt.getText().toString();
            String pwd = pwdEt.getText().toString();
            boolean flag = judgeInput(tel,pwd);
            if (flag) {
                Map<String,String> map = new HashMap<>();
                map.put("phone",tel);
                map.put("password",pwd);
                postNetWork(URLUtils.login_url,map);
            }
        }
    };


    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        UserBean userBean = new Gson().fromJson(result,UserBean.class);
        int code = userBean.getCode();
        switch (code){
            case 200://登录成功
                Toast.makeText(getContext(),"登录成功！",Toast.LENGTH_SHORT).show();

                //保存个人信息
                UserBean.DataBean dataBean = userBean.getData();
                SaveUserUtils.saveUserToFile(getContext(),dataBean);

                //跳转个人中心界面
                Intent intent = new Intent();
                ComponentName componentName = new ComponentName("com.example.beikeyigou","com.example.beikeyigou.PersonCenterActivity");
                intent.setComponent(componentName);
                intent.putExtra("user",userBean);
                startActivity(intent);
                getActivity().finish();
                //intent.setClass(LoginFragment.this,RegisterFragment.class);

                break;
            case 201 :
                Toast.makeText(getContext(),"登录密码或手机号有问题",Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getContext(),"登录失败！",Toast.LENGTH_SHORT).show();
                break;

        }

    }

    //判断输入的内容是否符合要求
    private boolean judgeInput(String tel,String pwd) {

        if (TextUtils.isEmpty(tel) ||TextUtils.isEmpty(pwd) ){

            Toast.makeText(getContext(),"输入内容不可为空",Toast.LENGTH_SHORT).show();
            return false;
        }

        //判断输入的电话号码是否满足号码格式
        String regex = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
        if (!tel.matches(regex)){
            Toast.makeText(getContext(),"输入的电话号码不合法",Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;

    }

}